<template>
  <div id="login">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px" class="login-box">
      <h3 class="login-title ">欢迎登录</h3>
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name" placeholder = "请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="passwd">
        <el-input type="password" v-model="form.passwd" placeholder = "请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('ruleForm')">登录</el-button>
        <router-link to='/registerinfo'>
          <el-button type="primary">注册</el-button>
        </router-link>
        <router-view/>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {login} from "../api/api";

export default {
  name: "Login",
  data() {
    return {
      form: {
        name: '',
        passwd: ''
      },
      rules: {
        name: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        passwd: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.form.name,this.form.passwd).then(response =>{
            if(response.data.sysHead.retStatus == 'F'){
              alert('用户名密码错误'+response.data.sysHead.resultList[0].retMessage);
              this.form.name = null;
              this.form.passwd = null;
            }else{
              this.$router.push({name:'index',params:{userId:response.data.userInfoList[0].id,userName:response.data.userInfoList[0].userName}});
            }
          }).catch(function (error) {
            console.log(error);
          })
        } else {
          this.$router.push('/');
        }
      })
    }
  }
}
</script>

<style scoped>
  .login-box {
    /*background-color: #b3e19d;*/
    /*宽度*/
    width: 400px;
    /*边距*/
    margin: 200px auto;
    /*边框*/
    border: 1px solid #2c3e50;
    /*内边距*/
    padding: 40px;
    /*边框圆角*/
    border-radius: 10px;
    /*阴影*/
    box-shadow: 0 0 30px #2c3e50;
  }

  .login-title {
  /*文本居中*/
  text-align: center;
  }
</style>
